﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Xml;
using System.Xml.Serialization;

namespace FGrubert.CrmQueueMonitor.WinForms.InfoMonitor
{
    /// <summary>
    /// The configuration of an information window
    /// </summary>
    public class WindowConfiguration
    {
        private Guid _id = Guid.NewGuid();
        /// <summary>
        /// Gets or sets the internal ID of the window
        /// </summary>
        public Guid Id
        {
            get { return _id; }
            set { _id = value; }
        }

        private List<InfoItem> _infos = new List<InfoItem>();
        /// <summary>
        /// Get or sets the infos to show
        /// </summary>
        public List<InfoItem> Infos
        {
            get { return _infos; }
            set { _infos = value; }
        }

        private int _refreshInterval = 20;
        /// <summary>
        /// Gets or sets the interval the information show be refreshed
        /// </summary>
        public int RefreshInterval
        {
            get { return _refreshInterval; }
            set { _refreshInterval = value; }
        }

        private bool _alwaysOnTop = false;
        /// <summary>
        /// Gets or sets if the window should be always on top
        /// </summary>
        public bool AlwaysOnTop
        {
            get { return _alwaysOnTop; }
            set { _alwaysOnTop = value; }
        }

        private string _windowTitle = "New Information Monitor Window";
        /// <summary>
        /// Gets or sets the title of the window
        /// </summary>
        public string WindowTitle
        {
            get { return _windowTitle; }
            set { _windowTitle = value; }
        }

        private System.Drawing.Point _lastPosition = new System.Drawing.Point(0, 0);
        /// <summary>
        /// Gets or sets the last position of the window
        /// </summary>
        public System.Drawing.Point LastPosition
        {
            get { return _lastPosition; }
            set { _lastPosition = value; }
        }

        private System.Drawing.Size _lastSize = new System.Drawing.Size(800, 600);
        /// <summary>
        /// Gets or sets the last size of the window
        /// </summary>
        public System.Drawing.Size LastSize
        {
            get { return _lastSize; }
            set { _lastSize = value; }
        }

        private bool _visible = true;
        /// <summary>
        /// If the window is visible or not
        /// </summary>
        public bool Visible
        {
            get { return _visible; }
            set { _visible = value; }
        }


        private FrmInfo _form;
        /// <summary>
        /// Gets or sets the form that is show to the user
        /// </summary>
        internal FrmInfo Form
        {
            get
            {
                return _form; 
            }
            set { _form = value; }
        }


        /// <summary>
        /// Serialize an array of WindowConfiguration objects to a string
        /// </summary>
        /// <param name="windowConfigs">An array of WindowConfiguration</param>
        /// <returns>The array aerialized as a string</returns>
        public static string Serialize(WindowConfiguration[] windowConfigs)
        {
            StringBuilder sb = new StringBuilder();
            XmlWriterSettings writerSettings = new XmlWriterSettings();
            writerSettings.Indent = true;
            writerSettings.OmitXmlDeclaration = true;
            using (XmlWriter writer = XmlWriter.Create(sb, writerSettings))
            {
                XmlSerializer serializer = new XmlSerializer(typeof(WindowConfiguration[]));
                serializer.Serialize(writer, windowConfigs);
            }
            return sb.ToString();
        }

        /// <summary>
        /// Serialize an WindowConfiguration base objects to a xml document
        /// </summary>
        /// <param name="windowConfigs">An array of WindowConfiguration</param>
        /// <returns>The array aerialized as a xml document</returns>
        public static XmlDocument SerializeToXmlNode(WindowConfiguration[] windowConfigs)
        {
            string serialized = Serialize(windowConfigs);
            XmlDocument xmlDoc = new XmlDocument();
            xmlDoc.LoadXml(serialized);
            return xmlDoc;
        }

        /// <summary>
        /// Deserializes a string back to an array of WindowConfiguration
        /// </summary>
        /// <param name="str">The serialized array</param>
        /// <returns>An array of WindowConfiguration</returns>
        public static WindowConfiguration[] Deserialize(string str)
        {
            using (TextReader reader = new StringReader(str))
            {
                XmlSerializer serializer = new XmlSerializer(typeof(WindowConfiguration[]));
                return (WindowConfiguration[])serializer.Deserialize(reader);
            }
        }

        /// <summary>
        /// Deserializes a xml document back to an array of WindowConfiguration
        /// </summary>
        /// <param name="xmlDoc">The serialized xml document</param>
        /// <returns>An array of WindowConfiguration</returns>
        public static WindowConfiguration[] Deserialize(XmlDocument xmlDoc)
        {
            using (TextReader reader = new StringReader(xmlDoc.OuterXml))
            {
                XmlSerializer serializer = new XmlSerializer(typeof(WindowConfiguration[]));
                return (WindowConfiguration[])serializer.Deserialize(reader);
            }
        }
    }
}
